﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace hamster
{
    public class Part
    {
        public float radius;
        public Vector3 pos;
        public Vector3 vel;
        public float life;
        public float h;

    }

    class Particles
    {

        Texture2D glowTexture;
        Effect glowEffect;
        public static Particles instance = new Particles();

        List<Part> parts = new List<Part>();
        public void Add(Vector3 position, Vector3 velocity, float _life, float _scale, float hue) {

            if (parts.Count > 100) return;
            parts.Add(new Part { pos = position, vel = velocity , life = _life, radius = _scale, h = hue});
        }
        public void Draw(GraphicsDevice device)
        {
            
            if (parts.Count == 0) return;
            VertexPositionNormalTexture[] _debug = new VertexPositionNormalTexture[parts.Count * 6];
            
            for(int i = 0; i < parts.Count; i++) {

                float zz = -3;
                int bi = i * 6;
                Part pa = parts[i];
                pa.pos.Z = zz;
                _debug[bi + 0] = new VertexPositionNormalTexture { Position = new Vector3(-1f, -1f, 0f) * pa.radius + pa.pos, TextureCoordinate = new Vector2(0, 1), Normal = new Vector3(pa.life,pa.h,0) };
                _debug[bi + 1] = new VertexPositionNormalTexture { Position = new Vector3(1f, -1f, 0f) * pa.radius + pa.pos, TextureCoordinate = new Vector2(1, 1), Normal = new Vector3(pa.life, pa.h, 0) };
                _debug[bi + 2] = new VertexPositionNormalTexture { Position = new Vector3(1f, 1f, 0f) * pa.radius + pa.pos, TextureCoordinate = new Vector2(1, 0), Normal = new Vector3(pa.life, pa.h, 0) };

                _debug[bi + 3] = new VertexPositionNormalTexture { Position = new Vector3(-1f, -1f, 0f) * pa.radius + pa.pos, TextureCoordinate = new Vector2(0, 1), Normal = new Vector3(pa.life, pa.h, 0) };
                _debug[bi + 4] = new VertexPositionNormalTexture { Position = new Vector3(1f, 1f, 0f) * pa.radius + pa.pos, TextureCoordinate = new Vector2(1, 0), Normal = new Vector3(pa.life, pa.h, 0) };
                _debug[bi + 5] = new VertexPositionNormalTexture { Position = new Vector3(-1f, 1f, 0f) * pa.radius + pa.pos, TextureCoordinate = new Vector2(0, 0), Normal = new Vector3(pa.life, pa.h, 0) };
            }


            device.RenderState.DepthBufferWriteEnable = false;
            device.RenderState.AlphaBlendEnable = true;
            device.RenderState.SourceBlend = Blend.SourceAlpha;
            device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;


            glowEffect.Parameters["View"].SetValue(Game1.viewMatrix);
            glowEffect.Parameters["Projection"].SetValue(Game1.projectionMatrix);

            glowEffect.Parameters["UserTexture"].SetValue(glowTexture);
            glowEffect.Begin();

            glowEffect.CurrentTechnique.Passes[0].Begin();
            device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, _debug, 0, _debug.Length / 3);
            glowEffect.CurrentTechnique.Passes[0].End();

            glowEffect.End();

            device.RenderState.AlphaBlendEnable = false;
            device.RenderState.DepthBufferWriteEnable = true;
        }
        public void Update(float dt)
        { 
            foreach (Part p in parts)
            {
                p.vel.Y -= 5.5f * dt;
                p.pos += p.vel * dt;
                p.life -= dt;
            }

            FIlter();
        }
        public void FIlter()
        {

            parts.RemoveAll(i => i.life < 0);
        }
        public void Create(ContentManager content)
        {
            if (glowEffect != null) glowEffect.Dispose();
            if (glowTexture != null) glowTexture.Dispose();

            glowEffect = content.Load<Effect>("particles");
            glowTexture = content.Load<Texture2D>("color_particle");
        }
    }
}
